﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using Gnosis.Alexandria.Data;


namespace Gnosis.Alexandria.Views
{
    public partial class MainTabView
        : ViewControlBase
    {
        public MainTabView()
        {
            InitializeComponent();
        }

        private class TabAndView
        {
            public TabPage TabPage { get; set; }
            public IViewOld View { get; set; }
        }

        private IDictionary<string, TabAndView> _tabs = new Dictionary<string, TabAndView>();

        private void AddSearchTab(string viewName)
        {
            var tab = new TabPage();
            tab.Text = "New Tab";
            var view = new SearchView();
            view.Controller = this.Controller;
            view.Name = viewName;
            view.Dock = DockStyle.Fill;

            tab.Controls.Add(view);
            var tabAndView = new TabAndView { TabPage = tab, View = view };
            _tabs.Add(view.Name, tabAndView);
            tabControl.Controls.Add(tab);
            tabControl.SelectedTab = tab;
        }

        private void AddEditArtistTab(string viewName)
        {
            var tab = new TabPage();
            tab.Text = "Edit Artist";
            var view = new ArtistEditView();
            view.Controller = this.Controller;
            view.Name = viewName;
            view.Dock = DockStyle.Fill;
            
            tab.Controls.Add(view);
            var tabAndView = new TabAndView { TabPage = tab, View = view };
            _tabs.Add(view.Name, tabAndView);
            tabControl.Controls.Add(tab);
            tabControl.SelectedTab = tab;
        }

        private void AddBrowseTab(string viewName, string siteName)
        {
            var tab = new TabPage();
            tab.Text = "Search " + siteName;
            var view = new BrowseView();
            view.Controller = this.Controller;
            view.Name = viewName;
            view.Dock = DockStyle.Fill;

            tab.Controls.Add(view);
            var tabAndView = new TabAndView { TabPage = tab, View = view };
            _tabs.Add(view.Name, tabAndView);
            tabControl.Controls.Add(tab);
            tabControl.SelectedTab = tab;
        }

        private void AddMediaPlayerTab(string viewName)
        {
            var tab = new TabPage();
            tab.Text = "Media Player";
            var view = new MediaPlayerView();
            view.Controller = this.Controller;
            view.Name = viewName;
            view.Dock = DockStyle.Fill;

            tab.Controls.Add(view);
            var tabAndView = new TabAndView { TabPage = tab, View = view };
            _tabs.Add(view.Name, tabAndView);
            tabControl.Controls.Add(tab);
            tabControl.SelectedTab = tab;
        }

        private static string GetNewViewName()
        {
            return Guid.NewGuid().ToString();
        }

        protected override void HandleMessage(IMessageOld message)
        {
            switch (message.Type)
            {
                case MessageTypes.RequestMainTabViewNewTab:
                {
                    AddSearchTab(Guid.NewGuid().ToString());
                    break;
                }
                case MessageTypes.RequestMainTabViewCloseTab:
                {
                    var tabName = (string)message.Body;
                    if (_tabs.ContainsKey(tabName))
                    {
                        var tab = _tabs[tabName].TabPage;
                        tabControl.TabPages.Remove(tab);
                    }
                    break;
                }
                case MessageTypes.RequestNavigateViewSearch:
                {
                    var request = message.Body as SearchRequestData;
                    _tabs[request.Source].TabPage.Text = string.Format("Search '{0}'", request.Search);
                    break;
                }
                case MessageTypes.ResponseNavigateViewSearch:
                {
                    var result = message.Body as SearchResultData;
                    var view = _tabs[result.Source].View;
                    view.Send(message);
                    break;
                }
                case MessageTypes.RequestSearchViewEdit:
                {
                    var requestData = message.Body as EditRequestData;
                    var view = _tabs[requestData.Source].View;
                    view.Send(message);
                    break;
                }
                case MessageTypes.ResponseRepositoryGetArtist:
                {
                    var viewName = GetNewViewName();
                    AddEditArtistTab(viewName);
                    _tabs[viewName].View.Send(message);
                    break;
                }
                case MessageTypes.RequestSearchAllMusicDotCom:
                case MessageTypes.RequestSearchLyricWiki:
                case MessageTypes.RequestSearchWikipedia:
                case MessageTypes.RequestSearchYouTube:
                case MessageTypes.RequestSearchGrooveshark:
                {
                    var searchData = message.Body as SearchRequestData;
                    _tabs[searchData.Source].View.Send(message);
                    break;
                }
                case MessageTypes.RequestBrowseViewLoad:
                {
                    var navigateData = message.Body as NavigateToUrlData;
                    var viewName = GetNewViewName();
                    AddBrowseTab(viewName, navigateData.Site);
                    _tabs[viewName].View.Send(message);
                    break;
                }
                case MessageTypes.RequestMediaPlayerLoad:
                {
                    var viewName = GetNewViewName();
                    AddMediaPlayerTab(viewName);
                    _tabs[viewName].View.Send(message);
                    break;
                }
                default:
                    break;
            }
        }

        private void tabControl_Selected(object sender, TabControlEventArgs e)
        {
            foreach (var item in _tabs)
            {
                if (item.Value.TabPage == e.TabPage)
                {
                    item.Value.View.Send(MessageTypes.ResponseMainTabViewTabSelected);
                    break;
                }
            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            HandleMessage(new Message(MessageTypes.RequestMainTabViewNewTab));
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            if (tabControl.SelectedTab != null)
            {
                foreach (var item in _tabs)
                {
                    if (item.Value.TabPage == tabControl.SelectedTab)
                    {
                        HandleMessage(new Message(MessageTypes.RequestMainTabViewCloseTab, item.Key));
                        break;
                    }
                }
            }
        }
    }
}
